package com.lagou.product.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.pojo.ProductVO;
import com.lagou.pojo.Products;
import com.lagou.pojo.ResponseResult;
import com.lagou.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/query/{id}")
    public Products queryById(@PathVariable Integer id){
         return productService.queryById (id);
    }

    @GetMapping("/queryByPage")
    public ResponseResult queryByPage(@RequestBody ProductVO productVO){
        PageInfo<Products> pageInfo = productService.queryByPage (productVO);
        //封装数据
        //System.out.print (pageInfo.getPageNum () + "22" + pageInfo.getPageSize ());
        ResponseResult responseResult = new ResponseResult ();
        responseResult.setMessage ("查询成功");
        responseResult.setStatus (200);
        responseResult.setSuccess (true);
        HashMap<String, Object> map = new HashMap<> ();
        map.put ("list",pageInfo.getList ());
        map.put ("total",pageInfo.getTotal ());
        responseResult.setContent (map);
        return responseResult;
    }

    @DeleteMapping("/delete/{id}")
    public Integer deleteById(@PathVariable Integer id){
        return productService.deleteById (id);
    }

    @PutMapping("/update")
    public Integer updateById(@RequestBody Products products){
        return productService.updateById (products);
    }

}
